﻿using BSChuangXu.API.Read.Appicant.Commend.MenuCommend;
using BSChuangXu.Domain.User;
using BSChuangXu.ErrorCode;
using BSChuangXu.Infrastructure.Interface;
using MediatR;

namespace BSChuangXu.API.Read.Appicant.CommendHandel.MenuCommandHandler
{
    /// <summary>
    /// 菜单名称唯一验证命令执行
    /// </summary>
    public class OnenessMenuNameCommandHandler : IRequestHandler<OneNessMenuNameCommend, APIResult<string>>
    {
        private readonly IBaseRepository<MenuModel> menuRepos;
        public OnenessMenuNameCommandHandler(IBaseRepository<MenuModel> menuRepos)
        {
            this.menuRepos = menuRepos;
        }
        /// <summary>
        /// 菜单名称唯一验证
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(OneNessMenuNameCommend request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var list = menuRepos.GetAll().Result.Where(x=>x.ParentId == request.ParentId 
            && x.MenuName == request.MenuName && x.IsDel == false).ToList();
            if(list.Count > 0)
            {
                result.Code = APIEnum.用户名重复;
                result.Msg = APIEnum.用户名重复.ToString();
                result.data = "1";//重复
            }
            else
            {
                result.Code = APIEnum.用户名唯一通过;
                result.Msg = APIEnum.用户名唯一通过.ToString();
                result.data = "2";//重复
            }
            return await Task.FromResult(result);
        }
    }
}
